home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / SHELL_SO / STRPATTE.C < prev    next >
Text File  |  1992-12-02  |  6KB  |  242 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 012290    :    Initial
  54.  *
  55.  *    Shell command line expansion pattern matching functions
  56.  *
  57.  *        *    Match any characters including none
  58.  *        ?    Match any single character
  59.  *       [╔]    Match any enclosed character
  60.  *            Note that ranges may be specified by [a-Z]
  61.  *       [╔]+    Match any enclosed character or sequence of chars.
  62.  */
  63.  
  64. #include    <ctype.h>
  65. #include    "SystemPub.h"
  66. #include    "Prefs.h"
  67.  
  68. static    Boolean    caseIns = FALSE;
  69.  
  70. /*******************************************************************/
  71.  
  72. void    SetCaseIns( Boolean flag )
  73. {
  74.     caseIns = flag;
  75. }
  76.  
  77. Boolean    GetCaseIns( void )
  78. {
  79.     return caseIns;
  80. }
  81.  
  82. /*******************************************************************/
  83.  
  84. int    strcmpi( char *s, char *t )
  85. {
  86.     for( ; tolower( *s ) == tolower( *t ); s++, t++ )
  87.         if( *s == '\0' )
  88.             return( 0 );
  89.             
  90.     return( *s - *t );
  91. }
  92.  
  93. int    strcmp( char *s, char *t )
  94. {
  95.     if( caseIns || ShellPrefs.caseIns )
  96.         return( strcmpi( s, t ) );
  97.  
  98.     for( ; *s == *t; s++, t++ )
  99.         if( *s == '\0' )
  100.             return( 0 );
  101.             
  102.     return( *s - *t );
  103. }
  104.  
  105. /*******************************************************************/
  106.  
  107. Boolean    IllegalPat( char *pattern )
  108. {
  109. int        c;
  110.  
  111.     while( *pattern )
  112.         {
  113.         c = *pattern++;
  114.         
  115.         if( c == '[' )    /* look for matching ']' */
  116.             {
  117.             while( c = *pattern++ )
  118.                 if( c == ']' )
  119.                     break;
  120.                     
  121.             if( !c )    /* didn't find matching ']' */
  122.                 return( TRUE );
  123.             }
  124.         }
  125.         
  126.     return( FALSE );
  127. }
  128.  
  129. /*******************************************************************/
  130.  
  131. Boolean    StrPatMatch( char *string, char *pattern )
  132. {
  133. char    c, n, f, e, cc;        /* character */
  134. char    *b;        /* beginning of bracket compare */
  135. Boolean    flag;
  136.  
  137.     if( IllegalPat( pattern ) )
  138.         return( FALSE );
  139.     
  140.     if( string == NULL || pattern == NULL )
  141.         return( FALSE );
  142.  
  143.     while( *pattern )
  144.         if ( *pattern == '?' )        /* match any single character */
  145.             {
  146.             string++;
  147.             pattern++;
  148.             }
  149.         else
  150.             {
  151.             c = *pattern++;        /* next character */
  152.             
  153.             if( c == '\\' )
  154.                 {
  155.                 switch( *pattern )
  156.                     {
  157.                     case '*':
  158.                     case '[':
  159.                     case ']':
  160.                     case '+':
  161.                     case '?':
  162.                         c = *pattern++;
  163.                         break;
  164.                         
  165.                     default:
  166.                         c = *pattern;
  167.                         break;
  168.                     }
  169.                 }
  170.             
  171.             if( caseIns || ShellPrefs.caseIns )
  172.                 c = tolower( c );
  173.             
  174.             if ( c == '*' )        /* match any sequence of characters */
  175.                 {
  176.                 n = *pattern;         /* matches until next char in pat string */
  177.                 
  178.                 if( caseIns || ShellPrefs.caseIns )        /* force case if needed */
  179.                     n = tolower( n );
  180. /*
  181.  *    Needs a fix for *[aaa] and *? and [asd]+ and **
  182.  */
  183.                  if(  caseIns || ShellPrefs.caseIns  )        /* force case if needed */
  184.                     while ( tolower( *string ) != n && *string )
  185.                         string++;
  186.                 else
  187.                     while ( *string != n && *string )
  188.                         string++;
  189.  
  190.                 if( !*string )            /* end of string */
  191.                     return( n == '\0' );
  192.                 }
  193.             else if ( c == '[' )
  194.                 {                       /* matches any char up to ']' in pattern */
  195.                 b = pattern;
  196.                 
  197.                 f = *b;                    /* for range checking */
  198.                 while( *b != ']' )
  199.                     {
  200.                     if( *b == '\\' )
  201.                         {
  202.                         if( *string == *++b )        /* we matched the character */
  203.                             break;
  204.                         }
  205.                     if( *string == *b )    /* we matched the character */
  206.                         {
  207.                         *string++;
  208.                         break;
  209.                         }
  210.                     else if( *b == '-' )
  211.                         {
  212.                         e = *++b;        /* end of range check */
  213.                         if( *string >= f && *string <= e )
  214.                             {
  215.                             string++;
  216.                             break;        /* we matched the range */
  217.                             }
  218.                         }
  219.                         
  220.                     f = *b++;
  221.                     }
  222.                 if( *b == ']' )            /* no match, exausted the patern */
  223.                     return( FALSE );
  224.                 else
  225.                     while( *pattern++ != ']' );
  226.                 }
  227.             else
  228.                 {
  229.                 cc = *string;        /* get character */
  230.                 if( caseIns || ShellPrefs.caseIns )        /* force case if needed */
  231.                     cc = tolower( cc );
  232.                 
  233.                 if ( c == cc )        /* compare */
  234.                     string++;
  235.                 else
  236.                     return( FALSE );    /* no match */
  237.                 }
  238.             }
  239.             
  240.     return( *string == '\0' );
  241. }
  242.